﻿function player_init() {
    player = {
        x: WIDTH * 0.5,
        y: HEIGHT * 0.75,
        left: false,
        right: false,
        up: false,
        down: false,
        shoot: false,
        shift: false,
        v1: 4,
        v2: 2,
        shoot_cd: 0,
        power: 0,
        life: 5,
        draw_time: 0,
    };
    
    window.onkeydown = player_keydown;
    window.onkeyup = player_keyup;
}

function player_powerup(power) {
    player.power += power;
    if (player.power > 4.00)
        player.power = 4.00;
}

function player_die() {
    player.life--;
    effect_1(player.x, player.y);
}

function player_update() {
    with (player) {
        if (shift) {
            if ((up || down) && (left || right)) {
                if (left) x -= v2 * 0.71;
                else if (right) x += v2 * 0.71;
                if (up) y -= v2 * 0.71;
                else if (down) y += v2 * 0.71;
            } else {
                if (left) x -= v2;
                else if (right) x += v2;
                else if (up) y -= v2;
                else if (down) y += v2;
            }
        } else {
            if ((up || down) && (left || right)) {
                if (left) x -= v1 * 0.71;
                else if (right) x += v1 * 0.71;
                if (up) y -= v1 * 0.71;
                else if (down) y += v1 * 0.71;
            } else {
                if (left) x -= v1;
                else if (right) x += v1;
                else if (up) y -= v1;
                else if (down) y += v1;
            }
        }
        
        if (x < 16) x = 16;
        else if (x > WIDTH - 16) x = WIDTH - 16;
        if (y < 24) y = 24;
        else if (y > HEIGHT - 24) y = HEIGHT - 24;
        
        // shot update
        if (shoot_cd > 0) {
            shoot_cd--;
        } else if (shoot) {
            shoot_cd = 3;
            if (shift) {
                if (player.power > 2) {
                    player_bullet_0(x - 3, y - 15, -pi * 0.5);
                    player_bullet_0(x + 3, y - 15, -pi * 0.5);
                    player_bullet_0(x - 10, y - 5, -pi * 0.5);
                    player_bullet_0(x + 10, y - 5, -pi * 0.5);
                } else if (player.power <= 2) {
                    player_bullet_0(x - 3, y - 15, -pi * 0.5);
                    player_bullet_0(x + 3, y - 15, -pi * 0.5);
                }
            } else {
                if (player.power > 2) {
                    player_bullet_0(x - 10, y - 30, -pi * 0.5);
                    player_bullet_0(x + 10, y - 30, -pi * 0.5);
                    player_bullet_0(x - 30, y - 10, -pi * 0.6);
                    player_bullet_0(x + 30, y - 10, -pi * 0.4);
                } else if (player.power <= 2) {
                    player_bullet_0(x - 10, y - 30, -pi * 0.5);
                    player_bullet_0(x + 10, y - 30, -pi * 0.5);
                }
            }
        }
    }
}

function player_draw() {
    if (player.draw_time >= 24)
        player.draw_time = 0;
    var index = floor(player.draw_time / 3);
    if (player.left)
        index += 8;
    else if (player.right)
        index += 16;
    player.draw_time++;
    
    context.drawImage(player_img, player_imgx[index], player_imgy[index], 32, 48, player.x - 15.5, player.y - 23.5, 32, 48);
    
    if (player.shift) {
        context.fillStyle = "rgb(255, 255, 255)";  
        context.beginPath();
        context.arc(player.x, player.y, 3, 0, 2 * Math.PI, true)
        context.fill();
    }
}

function player_keydown(e) {
    switch (e.keyCode) {
        case 27: // Esc
            pause ^= 1;
            break;
        case 90: // shoot
            player.shoot = true;
            break;
        case 16: // shift
            player.shift = true;
            break;
        case 37: // left
            player.left = true;
            break;
        case 38: // up
            player.up = true;
            break;
        case 39: // right
            player.right = true;
            break;
        case 40: // down
            player.down = true;
            break;
    }
}

function player_keyup(e) {
    switch (e.keyCode) {
        case 90: // shoot
            player.shoot = false;
            break;
        case 16: // shift
            player.shift = false;
            break;
        case 37: // left
            player.left = false;
            break;
        case 38: // up
            player.up = false;
            break;
        case 39: // right
            player.right = false;
            break;
        case 40: // down
            player.down = false;
            break;
    }
}

function player_bullet_0(px, py, pang) {
    player_bullet[player_bullet.length] = {x: px, y: py, v: 20, ang: pang, type: 0, power: 5, draw: bullet_draw_2};
}


function player_bullet_update() {
    var i = player_bullet.length;
    while (i--) {
        player_bullet[i].x += player_bullet[i].v * cos(player_bullet[i].ang);

        if (player_bullet[i].x < -20 || player_bullet[i].x >= WIDTH + 20) {
            player_bullet_remove(i);
            continue;
        }
        
        player_bullet[i].y += player_bullet[i].v * sin(player_bullet[i].ang);
        if (player_bullet[i].y < -20 || player_bullet[i].y >= HEIGHT + 20) {
            player_bullet_remove(i);
            continue;
        }
    }
}

function player_bullet_collide() {
    var i = player_bullet.length;
    while (i--) {
        var j = enemy.length;
        var flag = false;
        while (j--) {
            var xx = abs(player_bullet[i].x - enemy[j].x);
            var yy = abs(player_bullet[i].y - enemy[j].y);
            if (xx > 20 || yy > 20)
                continue;
            
            if (xx * xx + yy * yy < 400) {
                enemy[j].hp -= player_bullet[i].power;
                flag = true;
            }
        }
        if (flag)
            player_bullet_remove(i);
    }
}

function player_bullet_draw() {
    var i = player_bullet.length;
    while (i--) {
        player_bullet[i].draw();
    }
}

function player_bullet_remove(i) {
    player_bullet.splice(i, 1);
}
